﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Data;
using System.Configuration;
using DevFx.FrameWork.Context;
using DevFx.Model;

namespace DevFx.FrameWork.Data
{
    public class DbHelper
    {
        #region ADO.NET SQL
        //#region 变量
        ////EF框架使用的Provider未EntityClient
        //private static string dbConnectionString =  ConfigurationManager.ConnectionStrings["Entities"].ConnectionString;
        //private static string dbProviderName = ConfigurationManager.ConnectionStrings["Entities"].ProviderName;
        ////private static string dbProviderName = "System.Data.SqlClient";
        ////private static string dbConnectionString = @"server=SHUAIQIN\QS;database=5176;uid=sa;pwd=Password01!";
        //private DbConnection connection;
        //#endregion

        //#region 公共方法
        //public DbHelper()
        //{
        //    this.connection = CreateConnection(DbHelper.dbConnectionString);
        //}
        //public DbHelper(string connectionString)
        //{
        //    this.connection = CreateConnection(connectionString);
        //}
        //public static DbConnection CreateConnection()
        //{
        //    DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DbHelper.dbProviderName);
        //    DbConnection dbconn = dbfactory.CreateConnection();
        //    dbconn.ConnectionString = DbHelper.dbConnectionString;
        //    return dbconn;
        //}
        //public static DbConnection CreateConnection(string connectionString)
        //{
        //    DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DbHelper.dbProviderName);
        //    DbConnection dbconn = dbfactory.CreateConnection();
        //    dbconn.ConnectionString = connectionString;
        //    return dbconn;
        //}

        //public DbCommand GetStoredProcCommond(string storedProcedure)
        //{
        //    DbCommand dbCommand = connection.CreateCommand();
        //    dbCommand.CommandText = storedProcedure;
        //    dbCommand.CommandType = CommandType.StoredProcedure;
        //    return dbCommand;
        //}
        //public DbCommand GetSqlStringCommond(string sqlQuery)
        //{
        //    DbCommand dbCommand = connection.CreateCommand();
        //    dbCommand.CommandText = sqlQuery;
        //    dbCommand.CommandType = CommandType.Text;
        //    return dbCommand;
        //}

        //#endregion

        //#region 增加参数
        //public void AddParameterCollection(DbCommand cmd, DbParameterCollection dbParameterCollection)
        //{
        //    foreach (DbParameter dbParameter in dbParameterCollection)
        //    {
        //        cmd.Parameters.Add(dbParameter);
        //    }
        //}
        //public void AddOutParameter(DbCommand cmd, string parameterName, DbType dbType, int size)
        //{
        //    DbParameter dbParameter = cmd.CreateParameter();
        //    dbParameter.DbType = dbType;
        //    dbParameter.ParameterName = parameterName;
        //    dbParameter.Size = size;
        //    dbParameter.Direction = ParameterDirection.Output;
        //    cmd.Parameters.Add(dbParameter);
        //}
        //public void AddInParameter(DbCommand cmd, string parameterName, DbType dbType, object value)
        //{
        //    DbParameter dbParameter = cmd.CreateParameter();
        //    dbParameter.DbType = dbType;
        //    dbParameter.ParameterName = parameterName;
        //    dbParameter.Value = value;
        //    dbParameter.Direction = ParameterDirection.Input;
        //    cmd.Parameters.Add(dbParameter);
        //}
        //public void AddReturnParameter(DbCommand cmd, string parameterName, DbType dbType)
        //{
        //    DbParameter dbParameter = cmd.CreateParameter();
        //    dbParameter.DbType = dbType;
        //    dbParameter.ParameterName = parameterName;
        //    dbParameter.Direction = ParameterDirection.ReturnValue;
        //    cmd.Parameters.Add(dbParameter);
        //}
        //public DbParameter GetParameter(DbCommand cmd, string parameterName)
        //{
        //    return cmd.Parameters[parameterName];
        //}

        //#endregion

        //#region 执行
        //public DataSet ExecuteDataSet(DbCommand cmd)
        //{
        //    DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DbHelper.dbProviderName);
        //    DbDataAdapter dbDataAdapter = dbfactory.CreateDataAdapter();
        //    dbDataAdapter.SelectCommand = cmd;
        //    DataSet ds = new DataSet();
        //    dbDataAdapter.Fill(ds);
        //    return ds;
        //}

        //public DataTable ExecuteDataTable(DbCommand cmd)
        //{
        //    DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DbHelper.dbProviderName);
        //    DbDataAdapter dbDataAdapter = dbfactory.CreateDataAdapter();
        //    dbDataAdapter.SelectCommand = cmd;
        //    DataTable dataTable = new DataTable();
        //    dbDataAdapter.Fill(dataTable);
        //    return dataTable;
        //}

        //public DbDataReader ExecuteReader(DbCommand cmd)
        //{
        //    cmd.Connection.Open();
        //    DbDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        //    return reader;
        //}
        //public int ExecuteNonQuery(DbCommand cmd)
        //{
        //    cmd.Connection.Open();
        //    int ret = cmd.ExecuteNonQuery();
        //    cmd.Connection.Close();
        //    return ret;
        //}

        //public object ExecuteScalar(DbCommand cmd)
        //{
        //    cmd.Connection.Open();
        //    object ret = cmd.ExecuteScalar();
        //    cmd.Connection.Close();
        //    return ret;
        //}
        //#endregion

        #endregion

        #region EF DBHelper
        public static DbConnection CreateEFConnection()
        {
           return   GetCurrentDb("Entities").Connection;
        }
        #region 公共方法
        /// <summary>
        /// 得到EF对应的DbContext 上下文
        /// </summary>
        /// <returns></returns>
        public static Entities GetCurrentDb( )
        {
          return  GetCurrentDb("Entities");
        }
        /// <summary>
        /// 通过名称获取当前数据库
        /// </summary>
        /// <param name="dbName">数据库名称</param>
        /// <returns>数据库实例</returns>
        public static Entities GetCurrentDb(string dbName)
        {
            IContext dbContext = ContextHelper.GetDBContext();
            Entities db;
            if (!dbContext.Contains(dbName))
            {
                lock (dbContext)
                {
                    db =new Entities();
                    dbContext.PutItem(dbName, db);
                }
            }
            else
            {
                db = dbContext.GetItem(dbName) as Entities;
            }
            return db;
        }
        public static void RemoveCurrentDb( )
        {
            RemoveCurrentDb("Entities");
        }
        public static void RemoveCurrentDb(string dbName)
        {
            IContext dbContext = ContextHelper.GetDBContext();
            if (dbContext.Contains(dbName))
            {
                dbContext.Remove(dbName);
            }
        }

        /// <summary>
        /// 清除当前上下文中所有的数据库
        /// </summary>
        public static void ClearCurrentDatabases()
        {
            IContext dbContext = ContextHelper.GetDBContext();
            ICollection<string> keys = dbContext.Items.Keys;
            foreach (string key in keys)
            {
                if (dbContext.Items[key] is Entities)
                    ((Entities)dbContext.Items[key]).Connection.Dispose();
                dbContext.Remove(key);
            }

        }
        #endregion
        #endregion


    }
}
